package com.qf.controller;

import com.qf.annotation.IsLogin;
import com.qf.aop.UserLocal;
import com.qf.entity.*;
import com.qf.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrderService orderService;

    /**
     * 跳转到订单编辑页面 - 必须登录才能访问
     *
     * AOP + 自定义注解 - 统一的登录验证
     *
     * 1、游客可访问的接口
     * 2、必须登录的接口
     * 3、登录可访问、游客也可访问的接口，但是权限不同
     *
     * @return
     */
    @RequestMapping("/toOrderEdit")
    @IsLogin(mustLogin = true)
    public R toOrderEdit(Integer rid){

        //获得当前的用户信息
        User user = UserLocal.getUser();
        System.out.println("接收到下单请求：" + rid + " 下单用户: " + user);
        return new R();
    }

    /**
     * 计算订单总价
     * @return
     */
    @RequestMapping("/getPrices")
    public R getPrices(PricesParams pricesParams){
        PricesResult prices = orderService.getPrices(pricesParams);
        return new R().setData(prices);
    }

    /**
     * 下单的业务
     * @return
     */
    @IsLogin(mustLogin = true)
    @RequestMapping("/createOrders")
    public R createOrders(Orders orders, PricesParams pricesParams){
        System.out.println("下单业务触发，生成订单对象：" + orders);
        String orderid = orderService.createOrders(orders, pricesParams);
        return new R().setData(orderid);
    }
}
